package com.saturn.titan.web.controller.baseinfo;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.saturn.titan.core.bean.PlatformEmp;
import com.saturn.titan.core.bean.PlatformEmpTreeData;
import com.saturn.titan.core.bean.PlatformRole;
import com.saturn.titan.core.comm.CRMCache;
import com.saturn.titan.core.comm.ResponseBean;
import com.saturn.titan.core.service.EmpService;
import com.saturn.titan.core.service.RoleService;
import com.saturn.titan.web.utils.CRMUtil;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/emp")
public class EmpController {

	@Autowired
	private EmpService empService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) throws ParseException {
		return "baseinfo/emp/edit";
	}
	
	@RequestMapping(value = "delete",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean delete(@RequestParam(value="id")Long id,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			empService.deleteNode(id);
			rb.setResult(ResponseBean.RESULT_OK);
		} catch (Exception e) {
			e.printStackTrace();
			rb.setResult(ResponseBean.RESULT_NG);
			rb.setMeg(e.getMessage());
		}
		return rb;
	}
	
	@RequestMapping(value = "add",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean add(@Valid
			PlatformEmpTreeData emp,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			PlatformEmp a = new PlatformEmp();
			a.setAccount(emp.getAccount());
			a.setEmpName(emp.getEmpName());
			a.setOrgId(CRMUtil.getCurrentUserOrgId());
			a.setSuperEmpId(emp.getSuperEmpId());
			a.setEmpNo(emp.getEmpNo());
			a.setCrtDate(new Date());
			a.setAreaId(emp.getAreaId());
			List<Long> roleIds = Lists.newArrayList();
			if(emp.getRoleIds() != null){
				String[] ids = emp.getRoleIds().split(",");
				for(String id : ids){
					if(id.length() > 0){
						roleIds.add(Long.valueOf(id));
					}
				}
			}
			
			a.setPassword(emp.getPassword());
			CRMUtil.entryptEmpPassword(a);
			a.setCrtOpr(CRMUtil.getCurrentUser().id);
			empService.add(a,roleIds);
			rb.setResult(ResponseBean.RESULT_OK);
			rb.setData(a);
		} catch (Exception e) {
			e.printStackTrace();
			rb.setResult(ResponseBean.RESULT_NG);
			rb.setMeg(e.getMessage());
		}
		return rb;
	}
	
	@RequestMapping(value = "update",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean update(@Valid
			PlatformEmpTreeData emp,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			PlatformEmp a = new PlatformEmp();
			a.setId(emp.getId());
			a.setEmpName(emp.getEmpName());
			if(emp.getPassword() != null && !emp.getPassword().equals("*******") ){
				a.setPassword(emp.getPassword());
				CRMUtil.entryptEmpPassword(a);
			}
			
			List<Long> roleIds = null;
			if(emp.getRoleIds() != null && !emp.getRoleIds().equals("NoEdit")){
				roleIds = Lists.newArrayList();
				String[] ids = emp.getRoleIds().split(",");
				for(String id : ids){
					if(id.length() > 0){
						roleIds.add(Long.valueOf(id));
					}
				}
			}
			a.setAreaId(emp.getAreaId());
			a.setSuperEmpId(emp.getSuperEmpId());
			a.setVersion(emp.getVersion());
			a.setLstEdtDate(new Date());
			a.setLstEdtOpr(CRMUtil.getCurrentUser().id);
			
			empService.update(a,roleIds);
			rb.setResult(ResponseBean.RESULT_OK);
			rb.setData(a);
		} catch (Exception e) {
			e.printStackTrace();
			Throwable tmp = CRMUtil.getDataHaveChangedException(e);
			if(tmp != null){
				rb.setMeg(tmp.getMessage());
			}else{
				rb.setMeg(e.getMessage());
			}
			rb.setResult(ResponseBean.RESULT_NG);
		}
		return rb;
	}
	
	@RequestMapping(value = "select")
	@ResponseBody
	public Map<String,Object> select(@RequestParam(value="empName") String empName,
			@RequestParam(value="empNo") String empNo) throws ParseException {
		
		PlatformEmp ba = new PlatformEmp();
		if(!CRMUtil.blankStr(empName)){
			ba.setEmpName(empName);
		}
		if(!CRMUtil.blankStr(empNo)){
			ba.setEmpNo(empNo);
		}
		
		List<PlatformEmp> baseEmps = empService.findEmp(ba);
		List<PlatformEmpTreeData> result = Lists.newArrayList();
		PlatformEmpTreeData tmp = null;
		for(PlatformEmp emp : baseEmps){
			tmp = new PlatformEmpTreeData();
			tmp.setId(emp.getId());
			tmp.setEmpName(emp.getEmpName());
			tmp.setOrgId(emp.getOrgId());
			tmp.setDisplayOrg(CRMCache.displayOrg(emp.getOrgId()));
			tmp.setEmpNo(emp.getEmpNo());
			tmp.setAccount(emp.getAccount());
			tmp.setPassword("*******");
			tmp.setDisplayRole(changeRoleStr(roleService.findRolesByEmpId(emp.getId())));
			tmp.setCrtOpr(emp.getCrtOpr());
			tmp.setCrtDate(emp.getCrtDate());
			tmp.setLstEdtDate(emp.getLstEdtDate());
			tmp.setLstEdtOpr(emp.getLstEdtOpr());
			tmp.setIconCls("icon-client");
			tmp.set_parentId(emp.getSuperEmpId());
			tmp.setSuperEmpId(emp.getSuperEmpId());
			tmp.setDisplayArea(CRMCache.displayArea(emp.getAreaId()));
			tmp.setDisplaySuperEmp(CRMCache.displayOpr(emp.getSuperEmpId()));
			tmp.setVersion(emp.getVersion());
			tmp.setDisplayCrtOpr(CRMCache.displayOpr(emp.getCrtOpr()));
			tmp.setDisplayLstEdtOpr(CRMCache.displayOpr(emp.getLstEdtOpr()));
			result.add(tmp);
		}
		
		Map<String,Object> r = Maps.newHashMap();
		r.put("total", result.size());
		r.put("rows", result);
		r.put("foot", "");
		return r;
	}

	private String changeRoleStr(List<PlatformRole> roles) {
		String res = " ";
		for(PlatformRole r : roles){
			res += r.getRoleName()+",";
		}
		return res.substring(0,res.length() - 1);
	}
	
}
